package cn.wildfire.chat.app.main;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import cn.wildfire.chat.app.extendsion.classes.ResponseEntity;
import cn.wildfire.chat.app.extendsion.classes.ResponsePhoneNumberUserInfo;
import cn.wildfire.chat.app.extendsion.classes.SecurityAnswerEntity;
import cn.wildfire.chat.app.extendsion.classes.SecurityQuestionEntity;
import cn.wildfire.chat.app.extendsion.https.OkHttpRequest;
import cn.wildfire.chat.app.extendsion.https.ReqCallBack;
import cn.wildfire.chat.app.extendsion.https.ServerUrlClient;
import cn.wildfire.chat.app.extendsion.ui.ToastTip;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;
import cn.wildfire.chat.kit.WfcBaseActivity;

/**
 * Create by HEKE, 2019/10/17 14:40
 * TODO 设置密保问题
 */
public class SecurityQuestionSubmitActivity extends WfcBaseActivity {

    @BindView(R2.id.security_question_one)
    TextView securityQuestionOne;
    @BindView(R2.id.et_security_question_one)
    EditText etSecurityQuestionOne;
    @BindView(R2.id.security_question_two)
    TextView securityQuestionTwo;
    @BindView(R2.id.et_security_question_two)
    EditText etSecurityQuestionTwo;
    @BindView(R2.id.security_question_three)
    TextView securityQuestionThree;
    @BindView(R2.id.et_security_question_three)
    EditText etSecurityQuestionThree;
    @BindView(R2.id.btn_set_security_question_finish)
    Button btnSetSecurityQuestionFinish;
    private SecurityQuestionEntity securityQuestionEntity;
    ResponsePhoneNumberUserInfo responsePhoneNumberUserInfo;

    MyHandler myHandler = new MyHandler(this);

    private static class MyHandler extends Handler {
        private WeakReference<SecurityQuestionSubmitActivity> activityWeakReference;
        private SecurityQuestionEntity securityQuestionEntity;

        public MyHandler(SecurityQuestionSubmitActivity activity) {
            activityWeakReference = new WeakReference<SecurityQuestionSubmitActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            SecurityQuestionSubmitActivity activity = activityWeakReference.get();
            if (activity != null) {
                switch (msg.what) {
                    case 1:
                        securityQuestionEntity = (SecurityQuestionEntity) msg.obj;

                        activity.btnSetSecurityQuestionFinish.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                String answerOne = activity.etSecurityQuestionOne.getText().toString().trim();
                                String answerTwo = activity.etSecurityQuestionTwo.getText().toString().trim();
                                String answerThree = activity.etSecurityQuestionThree.getText().toString().trim();

                                /*if (TextUtils.isTextEmpty(answerOne) || TextUtils.isTextEmpty(answerTwo) || TextUtils.isTextEmpty(answerThree)) {
                                    ToastTip.tip(activity.getString(R.string.security_question_have_to_fill_in_all));
                                    return;
                                }*/

                                HashMap<String, Object> params = new HashMap<>();
                                List<String> answers = new ArrayList<>();
                                List<SecurityAnswerEntity> securityAnswerEntityList = new ArrayList<>();
                                if (answerOne == null || answerOne.equals("")) {
                                } else {
                                    answers.add(answerOne);
                                    SecurityAnswerEntity securityAnswerEntityOne = new SecurityAnswerEntity(securityQuestionEntity.getResult().get(0).getId(), answerOne);
                                    securityAnswerEntityList.add(securityAnswerEntityOne);
                                }
                                if (answerTwo == null || answerTwo.equals("")) {
                                } else {
                                    answers.add(answerTwo);
                                    SecurityAnswerEntity securityAnswerEntityTwo = new SecurityAnswerEntity(securityQuestionEntity.getResult().get(1).getId(), answerTwo);
                                    securityAnswerEntityList.add(securityAnswerEntityTwo);
                                }
                                if (answerThree == null || answerThree.equals("")) {
                                } else {
                                    answers.add(answerThree);
                                    SecurityAnswerEntity securityAnswerEntityThree = new SecurityAnswerEntity(securityQuestionEntity.getResult().get(2).getId(), answerThree);
                                    securityAnswerEntityList.add(securityAnswerEntityThree);
                                }
                                params.put("answers", answers);
                                params.put("encryptedRelations", securityAnswerEntityList);
                                params.put("uid", activity.responsePhoneNumberUserInfo.getResult().getUid());

                                OkHttpRequest.getInstance(activity).requestAsynJsonObject(ServerUrlClient.REQEUST_SECURITY_PASSWORD_VERIFICATION, params, new ReqCallBack<Object>() {
                                    @Override
                                    public void onReqSuccess(Object result) {
                                        if (!TextUtils.isTextEmpty(result.toString())) {
                                            try {
                                                ResponseEntity<String, String> responseEntity = new ResponseEntity<>(result.toString());
                                                if (responseEntity.isSuccess()) {
                                                    ToastTip.tip(activity.getString(R.string.verification_success));
                                                    Intent intent = new Intent(activity, SetNewPasswordActivity.class);
                                                    intent.putExtra("UserInfo", activity.responsePhoneNumberUserInfo);
                                                    activity.startActivity(intent);
                                                } else {
                                                    ToastTip.tip(responseEntity.message);
                                                }
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }

                                    @Override
                                    public void onReqFailed(String errorMsg) {
                                        Toast.makeText(activity, "验证失败!", Toast.LENGTH_SHORT).show();
                                    }
                                }).request();
                            }
                        });
                        break;
                    default:
                        break;
                }
            }
        }
    }


    @Override
    protected void afterViews() {
        super.afterViews();
        Intent intent = getIntent();
        responsePhoneNumberUserInfo = intent.getParcelableExtra("UserInfo");
        responsePhoneNumberUserInfo.getResult().getMobile();
        getSecurityQuestion();
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_security_question;
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 2);
        setReplaceTitle(null, R.string.security_question);
        setSearchLayoutVisibility(false);
    }

    private void getSecurityQuestion() {
        OkHttpRequest.getInstance(this).requestAsynJsonObject(ServerUrlClient.GET_SECURITY_QUESTION, null, new ReqCallBack<Object>() {
            @Override
            public void onReqSuccess(Object result) {
                if (!TextUtils.isTextEmpty(result.toString())) {
                    try {
                        Gson gson = new Gson();
                        securityQuestionEntity = gson.fromJson(result.toString(), SecurityQuestionEntity.class);
                        securityQuestionOne.setText(securityQuestionEntity.getResult().get(0).getProblem());
                        securityQuestionTwo.setText(securityQuestionEntity.getResult().get(1).getProblem());
                        securityQuestionThree.setText(securityQuestionEntity.getResult().get(2).getProblem());

                        Message message = myHandler.obtainMessage();
                        message.what = 1;
                        message.obj = securityQuestionEntity;
                        myHandler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onReqFailed(String errorMsg) {

            }
        }).request();
    }
}
